package com.ruoyi.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.contract.domain.ConfTypeEnum;
import com.ruoyi.contract.domain.IceFlow;
import com.ruoyi.contract.service.IceFlowService;
import com.ruoyi.contract.mapper.IceFlowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 姚煜峰
* @description 针对表【ICE_FLOW】的数据库操作Service实现
* @createDate 2023-07-07 11:18:05
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class IceFlowServiceImpl extends ServiceImpl<IceFlowMapper, IceFlow>
    implements IceFlowService{

    @Override
    public AjaxResult insert(IceFlow flow) throws Exception {

        check(flow);

        return save(flow)?AjaxResult.success():AjaxResult.error();
    }

    @Override
    public AjaxResult update(IceFlow flow) throws Exception {
        check(flow);
        return updateById(flow)?AjaxResult.success():AjaxResult.error();
    }

    private void check(IceFlow flow) throws Exception {

        if(StringUtils.isEmpty(flow.getFlowName())){
            throw new Exception("节点名称不能为空");
        }

        if(StringUtils.isEmpty(flow.getDatabaseName())){
            throw new Exception("数据库名称不能为空");
        }

        if(StringUtils.isEmpty(flow.getConfType())){
            throw new Exception("配置类型不能为空");
        }

        QueryWrapper<IceFlow> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .eq(IceFlow::getDatabaseName,flow.getDatabaseName())
                .ne(StringUtils.isNotNull(flow.getId()),IceFlow::getId,flow.getId());

        List<IceFlow> list = list(queryWrapper);

        if(list.size()>0){
            throw new Exception("唯一校验失败,数据库名称已存在");
        }

        if(ConfTypeEnum.String.getCode().equals(flow.getConfType())){
            flow.setConfName(ConfTypeEnum.String.getInfo());
        }

        if(ConfTypeEnum.Number.getCode().equals(flow.getConfType())){
            flow.setConfName(ConfTypeEnum.Number.getInfo());
        }

        if(ConfTypeEnum.Date.getCode().equals(flow.getConfType())){
            flow.setConfName(ConfTypeEnum.Date.getInfo());
        }

        if(ConfTypeEnum.Amount.getCode().equals(flow.getConfType())){
            flow.setConfName(ConfTypeEnum.Amount.getInfo());
        }

        if(ConfTypeEnum.Ladder.getCode().equals(flow.getConfType())){
            flow.setConfName(ConfTypeEnum.Ladder.getInfo());
        }

    }
}




